﻿using CPPEI.Coolzon.Common;
using CPPEI.Coolzon.Designer.Event.NCL;
using CPPEI.Coolzon.Designtime.Metadata;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace CPPEI.Coolzon.Designer.Views
{
    public partial class SolutionSelectView
    {
        [Subscribe]
        public void EndGetSolutionListEvent(EndGetSolutionListEvent @event)
        {
            solutionModelList.Clear();
            if (@event.State == NCLEventState.Failure)
            {
                Dispatcher.Invoke(() =>
                {
                    _ctlwpListView.Visibility = System.Windows.Visibility.Collapsed;
                    _btnRetry.Visibility = System.Windows.Visibility.Visible;
                    _ctlTbMessage.Visibility = System.Windows.Visibility.Collapsed;
                });
            }
            else if (@event.State == NCLEventState.Finished)
            {
                if (@event.List != null && @event.List.Length > 0)
                {
                    foreach (var item in @event.List.OrderByDescending(p=>p.UpdateTime))
                    {
                        bool isExistInServer = item.IsExistSolution;
                        SolutionViewModel svm = (SolutionViewModel)item;
                        svm.IsLocalExist = LocalStorage.ExistSolution(UIContext.Current.LocalWorkSpace.LocalPath, item.Id);

                        if (!svm.IsLocalExist && !isExistInServer) continue;

                        this.Dispatcher.Invoke(() =>
                        {
                            _ctlTbMessage.Visibility = System.Windows.Visibility.Collapsed;
                            _ctlwpListView.Visibility = System.Windows.Visibility.Visible;
                            _btnRetry.Visibility = System.Windows.Visibility.Collapsed;
                            solutionModelList.Add(svm);
                        });
                    }
                    FilterSolution();
                    ReSizeColumnWidth();
                }
            }
        }
    }
}
